package cn.le.util;

import cn.le.exception.BizException;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Bean对象工具类
 * Created by zsq on 2017/7/26.
 */
public class BeanUtils extends org.springframework.beans.BeanUtils{

    /**
     * 把Bean中的属性复制到Map对象中
     * @param bean
     *      实体Bean对象
     * @return
     *      返回Map对象
     */
    public static Map<String,Object> populate(Object bean){
        Map<String,Object> map = new LinkedHashMap<>();
        if(bean != null){
            BeanWrapper beanWrapper = new BeanWrapperImpl(bean);
            PropertyDescriptor[] descriptor = beanWrapper.getPropertyDescriptors();
            for (PropertyDescriptor aDescriptor : descriptor) {
                String name = aDescriptor.getName();
                if ("class".equals(name)) continue;
                map.put(name, beanWrapper.getPropertyValue(name));
            }
        }
        return map;
    }

    /**
     * 把Bean中的属性复制到Map对象中
     * @param bean
     *      实体Bean对象
     * @return
     *      返回Map对象
     */
    public static Map<String,Object> populateLineMap(Object bean) throws BizException {
        Map<String,Object> map = new LinkedHashMap<>();
        if(bean != null){
            BeanWrapper beanWrapper = new BeanWrapperImpl(bean);
            PropertyDescriptor[] descriptor = beanWrapper.getPropertyDescriptors();
            for (PropertyDescriptor aDescriptor : descriptor) {
                String name = aDescriptor.getName();
                if ("class".equals(name)) continue;
                MapUtils.setPropertyFromMap(map, name, beanWrapper.getPropertyValue(name));
            }
        }
        return map;
    }

    /**
     * 把Bean中的属性复制到Map对象中
     * @param bean
     *      实体Bean对象
     * @return
     *      返回Map对象
     */
    public static Map<String,String> populateStringMap(Object bean){
        Map<String,String> map = new LinkedHashMap<>();
        if(bean != null){
            BeanWrapper beanWrapper = new BeanWrapperImpl(bean);
            PropertyDescriptor[] descriptor = beanWrapper.getPropertyDescriptors();
            for (PropertyDescriptor aDescriptor : descriptor) {
                String name = aDescriptor.getName();
                if ("class".equals(name)) continue;
                map.put(name, String.valueOf(beanWrapper.getPropertyValue(name)));
            }
        }
        return map;
    }

    /**
     * 把Bean对象中的属性复制到新实体Bean中
     * @param type
     *      实体Bean的类型
     * @param source
     *      源实体对象
     * @return
     *      实体Bean实例
     */
    public static <T> T copyProperties(Class<T> type,Object source){
        T obj = instantiateClass(type);
        copyProperties(source,obj);
        return obj;
    }

    /**
     * 把Map对象中的属性复制到实体Bean中
     * @param type
     *      实体Bean的类型
     * @param map
     *      Map对象
     * @return
     *      实体Bean实例
     */
    public static <T> T copyProperties(Class<T> type,Map<String,Object> map){
        T obj = instantiateClass(type);
        BeanWrapper beanWrapper = new BeanWrapperImpl(obj);
        beanWrapper.setPropertyValues(map);
        return obj;
    }

}
